package cn.edu.zafu.bigdata.XuShuXin.Exp1112;

import cn.edu.zafu.bigdata.x.wupeng.g.ReverseThread;
import cn.edu.zafu.bigdata.x.wupeng.g.SortThread;

import java.io.*;

/**
 * @author: aluvfy
 * @description: 第一题：完成课本Rhymingwords代码，验证文件管道操作，words文件存储在files目录下的words.txt
 * @date: 2024/11/12 10:19
 */
public class RhymingWords_xsx {
    public static void main(String[] args) throws IOException{
        FileReader words = new FileReader("files/words.txt");
        Reader rhymeWords = reverse(sort(reverse(words)));
        BufferedReader in = new BufferedReader(rhymeWords);
        String input;
        while ((input = in.readLine()) != null) {
            System.out.println(input);
        }
        in.close();
    }

    //创建管道，创建并启动单词逆序线程
    public static Reader reverse(Reader source) throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out = new PrintWriter(pipeOut);
        new ReverseThread(out, in).start();
        return pipeIn;
    }

    //创建管道，创建并启动单词排序线程
    public static Reader sort(Reader source) throws IOException{
        BufferedReader in = new BufferedReader(source);
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out = new PrintWriter(pipeOut);
        new SortThread(out, in).start();
        return pipeIn;
    }

}
